/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import crm.salesOrderProcessing.entity.LineItemEntity;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import mrp.productionPlanning.entity.BackorderEntity;
import mrp.productionPlanning.entity.DailyDetailEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class processSalesOrderSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    @EJB
    private DailyDetailSessionBean ddsb = new DailyDetailSessionBean();
    @EJB
    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
    @EJB
    BackorderSessionBean bosb = new BackorderSessionBean();
    
    public int leadTimeEstimation (int demandA, int demandB, int demandC, int demandD){
        int count  = 0;
        Calendar cal = new GregorianCalendar();
        int qA =demandA;
        int qB =demandB;
        int qC =demandC;
        int qD =demandD;
        
        qA -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        qB -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        qC -= icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        qD -= icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (qA <0) {qA = 0;}
        if (qB <0) {qB = 0;}
        if (qC <0) {qC = 0;}
        if (qD <0) {qD = 0;}        
        
        float time = qA/50 + qB/50 + qC/45 + qD/45;
        
        while (time > (ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity())+ ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime()){
            DailyDetailEntity day = ddsb.getDailyDetailByDate(cal.getTime());
            qA = qA - day.getScheduleA() + day.getReservedA();
            qB = qB - day.getScheduleB() + day.getReservedB();
            qC = qC - day.getScheduleC() + day.getReservedC();
            qD = qD - day.getScheduleD() + day.getReservedD();
            if (qA <0) {qA = 0;}
            if (qB <0) {qB = 0;}
            if (qC <0) {qC = 0;}
            if (qD <0) {qD = 0;}
            time = qA/50 + qB/50 + qC/45 + qD/45;
            time -= day.getLeftCapacity();
            time -= day.getLeftOvertime();
            count ++;
            cal.add(Calendar.DATE, 1);
        }
        
        return count;
    }
    
    public int leadTimeEstimation (DeliveryOrderEntity doe){
        
//        int count = 0;
//        Calendar cal = new GregorianCalendar();
//        float time = 0;
//        List<LineItemEntity> lineItems = doe.getLineItems();
//        
//        for (LineItemEntity lineItem: lineItems){
//            String name = lineItem.getProduct().getProductName();
//            if(name.equalsIgnoreCase("productA") || name.equalsIgnoreCase("productB")){
//                time += lineItem.getQuantity()/50;
//            }
//            else{
//                time += lineItem.getQuantity()/45;
//            }       
//        }
//        
//        while (availableCapacityUntil(cal.getTime())<time){
//            count ++;
//            cal.add(Calendar.DATE, 1);
//        }
//        count ++;
//        
//        return count;
        int count  = 0;
        Calendar cal = new GregorianCalendar();
        int qA =0;
        int qB =0;
        int qC =0;
        int qD =0;
        List<LineItemEntity> lineItems = doe.getLineItems();
        
        for (LineItemEntity lineItem : lineItems){
            if (lineItem.getProduct().getProductName().equalsIgnoreCase("productA")){
                qA += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productB")){
                qB += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productC")){
                qC += lineItem.getQuantity();
            }
            else if (lineItem.getProduct().getProductName().equalsIgnoreCase("productD")){
                qD += lineItem.getQuantity();
            }
        }
        qA -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Nuts");
        qB -= icsb.getCurrentInventoryLevel("Classic Nutri Bar-Fruits");
        qC -= icsb.getCurrentInventoryLevel("Premium Power Bar-Chocolate Nuts");
        qD -= icsb.getCurrentInventoryLevel("Premium Power Bar-Redcurrant Fruits");
        if (qA <0) {qA = 0;}
        if (qB <0) {qB = 0;}
        if (qC <0) {qC = 0;}
        if (qD <0) {qD = 0;}        
        
        float time = qA/50 + qB/50 + qC/45 + qD/45;
        
        while (time > (ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity())+ ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime()){
            DailyDetailEntity day = ddsb.getDailyDetailByDate(cal.getTime());
            qA = qA - day.getScheduleA() + day.getReservedA();
            qB = qB - day.getScheduleB() + day.getReservedB();
            qC = qC - day.getScheduleC() + day.getReservedC();
            qD = qD - day.getScheduleD() + day.getReservedD();
            if (qA <0) {qA = 0;}
            if (qB <0) {qB = 0;}
            if (qC <0) {qC = 0;}
            if (qD <0) {qD = 0;}
            time = qA/50 + qB/50 + qC/45 + qD/45;
            time -= day.getLeftCapacity();
            time -= day.getLeftOvertime();
            count ++;
            cal.add(Calendar.DATE, 1);
        }
        
        return count;
    }
    
    public float availableCapacityUntil(Date endDate){
        
        float capacity = 0;
        
        Calendar cal = new GregorianCalendar();
        
        Date current = cal.getTime();
        cal.setTime(current);
        
        while (cal.getInstance().before(endDate)){
            capacity += ddsb.getDailyDetailByDate(cal.getTime()).getLeftCapacity();
            capacity += ddsb.getDailyDetailByDate(cal.getTime()).getLeftOvertime();
            cal.add(Calendar.DATE, 1);
        }
        return capacity;
        
    }
    
    public boolean needBackorder(DeliveryOrderEntity doe){
        
        boolean out = true;
        Date dueDate = null;
        Calendar cal = new GregorianCalendar();
        float time = 0;
        int quantityA = 0;
        int quantityB = 0;
        int quantityC = 0;
        int quantityD = 0;
        List<LineItemEntity> lineitems = doe.getLineItems();
        for (LineItemEntity lineitem:lineitems){
            String name = lineitem.getProduct().getProductName();
            if (name.equalsIgnoreCase("productA")){
                quantityA += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("productB")){
                quantityB += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("productC")){
                quantityC += lineitem.getQuantity();
            }
            else if (name.equalsIgnoreCase("productD")){
                quantityD += lineitem.getQuantity();
            }
        }
        List<DailyDetailEntity> period = ddsb.getPeriod(Calendar.getInstance().getTime(), doe.getDeliveryDate());
        int plannedA = 0;
        int plannedB = 0;
        int plannedC = 0;
        int plannedD = 0;
        for(DailyDetailEntity day : period){
            plannedA += day.getScheduleA();
            plannedB += day.getScheduleB();
            plannedC += day.getScheduleC();
            plannedD += day.getScheduleD();
        }
        out = (quantityA > plannedA || quantityB > plannedB || quantityC > plannedC || quantityD > plannedD);
        
        BackorderEntity boe = new BackorderEntity();
        
        if(out){
            int demandA = quantityA - plannedA;
            int demandB = quantityB - plannedB;
            int demandC = quantityC - plannedC;
            int demandD = quantityD - plannedD;
            if (demandA < 0){
                demandA = 0;
            }
            if (demandB < 0){
                demandB = 0;
            }
            if (demandC < 0){
                demandC = 0;
            }
            if (demandD < 0){
                demandD = 0;
            }
            boe.setDemandA(demandA);
            boe.setDemandB(demandB);
            boe.setDemandC(demandC);
            boe.setDemandD(demandD);
            boe.setDueDate(boe.getDueDate());
            boe.setSalesOrders(boe.getSalesOrders());
            bosb.addBackorder(boe);
        }
        
        return out;
    }
    
    
    
}
